library RespawnControl initializer init 

	globals
		private trigger crepitusTrigger
		private trigger immortalTrigger
		private trigger ghoulTrigger
	endglobals

	private function CrepitusSpawnConditions takes nothing returns boolean
		return GetUnitTypeId(GetTriggerUnit())=='n00D' and GetRandomInt(0,2)==1
	endfunction

	private function CrepitusSpawnActions takes nothing returns nothing
		local real x = GetUnitX(GetTriggerUnit())
		local real y = GetUnitY(GetTriggerUnit())
		local unit u2 = GetKillingUnit()
		if GetUnitTypeId(u2)!='H000' then //not FT
			call PolledWait(GetRandomReal(4.,12.))
			call DestroyEffect(AddSpecialEffect("Objects\\Spawnmodels\\Undead\\UndeadBlood\\UndeadBloodNecromancer.mdl",x,y ))
			call CreateUnit(Player(13),'n00D',x,y,0.0)
			call TriggerSleepAction(0.)
			call CreateUnit(Player(13),'n00D',x+5.0,y+5.0,0.0)
		endif
		set u2 = null
	endfunction
	
	private function ImmortalSpawnConditions takes nothing returns boolean
		local integer i = GetUnitTypeId(GetTriggerUnit())
		return (i=='n003' or i=='n001') and GetRandomInt(0,udg_DMKillingSpree[99])!=1
	endfunction
	private function ImmortalSpawnActions takes nothing returns nothing
		local unit u = GetTriggerUnit()
		local unit u2 = GetKillingUnit()
		local unit u3 //revived immortal
		local real x = GetUnitX(u)
		local real y = GetUnitY(u)
		local integer id=GetUnitTypeId(u2)
		if id!='H003' and id!='H000' and id!='z00F' and id!='z00G' and id!='z009' then //not DM and FT
			call PolledWait(GetRandomReal(4.,12.))
			set u3 = CreateUnit(Player(14),udg_Immortal,x,y,0)
			if udg_SquadXP>75000 then
				call UnitAddAbility(u3,'A08H')
			endif
			set x = GetUnitX(u2)
			set y = GetUnitY(u2)
			call IssuePointOrder(u,"attack",x,y)
			call RemoveUnit(u)
			call PolledWait(GetRandomReal(1.,6.))
			call UnitRemoveAbility(u3,'A08H')
		endif
		set u = null
		set u2 = null
		set u3 = null
	endfunction

	private function GhoulSpawnConditions takes nothing returns boolean
		return IsUnitIdType(GetUnitTypeId(GetTriggerUnit()),UNIT_TYPE_SUMMONED)!=true and GetRandomInt(1,5)!=1
	endfunction

	private function GhoulSpawnActions takes nothing returns nothing
		local unit u = GetTriggerUnit()
		local integer i
		local real x = GetUnitX(u)
		local real y = GetUnitY(u)
		call PolledWait(GetRandomReal(3.,12.))
		call DestroyEffect(AddSpecialEffect("Objects\\Spawnmodels\\Undead\\UndeadBlood\\UndeadBloodNecromancer.mdl",x,y ))
		set u = CreateUnit(Player(14),udg_Ghoul,x,y,0.0)
		set i = udg_Sorted[GetRandomInt(1, udg_Players)]
		set x = GetUnitX(udg_Hero[i])
		set y = GetUnitY(udg_Hero[i])
		call IssuePointOrder(u,"attack",x,y)
		set u = null
	endfunction

	function SetImmortalSpawnEnabled takes boolean b returns nothing
		if b then
			call EnableTrigger(immortalTrigger)
		else
			call DisableTrigger(immortalTrigger)
		endif
	endfunction
	
	function SetCrepitusSpawnEnabled takes boolean b returns nothing
		if b then
			call EnableTrigger(crepitusTrigger)
		else
			call DisableTrigger(crepitusTrigger)
		endif
	endfunction
	
	function SetGhoulSpawnEnabled takes boolean b returns nothing
		if b then
			call EnableTrigger(ghoulTrigger)
		else
			call DisableTrigger(ghoulTrigger)
		endif
	endfunction
	
	private function init takes nothing returns nothing
		set crepitusTrigger = CreateTrigger()
		call DisableTrigger(crepitusTrigger)
		call TriggerRegisterPlayerUnitEventSimple(crepitusTrigger,Player(14),EVENT_PLAYER_UNIT_DEATH)
		call TriggerRegisterPlayerUnitEventSimple(crepitusTrigger,Player(13),EVENT_PLAYER_UNIT_DEATH)
		call TriggerAddCondition( crepitusTrigger, Condition(function CrepitusSpawnConditions) )
		call TriggerAddAction(crepitusTrigger,function CrepitusSpawnActions)
		
		set immortalTrigger = CreateTrigger()
		call DisableTrigger(immortalTrigger)
		call TriggerRegisterPlayerUnitEventSimple( immortalTrigger, Player(14), EVENT_PLAYER_UNIT_DEATH )
		call TriggerRegisterPlayerUnitEventSimple( immortalTrigger, Player(13), EVENT_PLAYER_UNIT_DEATH )
		call TriggerAddCondition( immortalTrigger, Condition( function ImmortalSpawnConditions ) )
		call TriggerAddAction(immortalTrigger,function ImmortalSpawnActions)
		
		set ghoulTrigger = CreateTrigger()
		call TriggerAddCondition(ghoulTrigger, Condition(function GhoulSpawnConditions) )
		call TriggerAddAction(ghoulTrigger,function GhoulSpawnActions)
		call TriggerRegisterPlayerUnitEventSimple(ghoulTrigger, Player(14), EVENT_PLAYER_UNIT_DEATH)
		call TriggerRegisterPlayerUnitEventSimple(ghoulTrigger, Player(13), EVENT_PLAYER_UNIT_DEATH)

	endfunction

endlibrary
